package cn.ncist.health_service_provider.generate;

import cn.ncist.health_interface.service.SetMealService;
import cn.ncist.health_service_provider.dao.CheckGroupMapper;
import cn.ncist.health_service_provider.dao.SetMealMapper;
import cn.ncist.pojo.CheckGroup;
import cn.ncist.pojo.CheckItem;
import cn.ncist.pojo.Setmeal;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成静态的html页面
 */
@Slf4j
@Component
public class GenerateHtml {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    private String outPutPath = "E:\\Project\\ncist_health\\health_parent\\health_mobile\\src\\main\\resources\\static";

    //生成套餐列表静态页面
    public void generateMobileStaticHtml() {
        //获取套餐的全部数据
        List<Setmeal> setmealList = setMealMapper.findAll();
        //生成套餐列表静态页面
        generateMobileSetmealListHtml(setmealList);
        //生成套餐详情静态页面（多个）
        generateMobileSetmealDetailHtml(setmealList);
    }

    //生成套餐列表静态页面
    public void generateMobileSetmealListHtml(List<Setmeal> setmealList) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("setmealList", setmealList);
        this.generateHtml("mobile_setmeal.ftl", "m_setmeal.html", map);
    }

    //生成套餐详情静态页面（多个）
    public void generateMobileSetmealDetailHtml(List<Setmeal> setmealList) {
        for (Setmeal setmeal : setmealList) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            //获取checkGroup信息
            List<CheckGroup> checkGroupList = setMealMapper.findCheckGroupsBySetmealId(setmeal.getId());
            //获取checkItem信息
            for (CheckGroup checkGroup : checkGroupList) {
                Integer checkGroupId = checkGroup.getId();
                List<CheckItem> checkItemList = checkGroupMapper.selectCheckItemFromTableCheckItem(checkGroupId);
                checkGroup.setCheckItems(checkItemList);
            }
            setmeal.setCheckGroups(checkGroupList);
            dataMap.put("setmeal", setmeal);
            this.generateHtml("mobile_setmeal_detail.ftl",
                    "setmeal_detail_" + setmeal.getId() + ".html",
                    dataMap);
        }
    }

    public void generateHtml(String templateName, String htmlPageName, Map<String, Object> dataMap) {
        // 创建配置类
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer out = null;
        try {
            //加载模板数据
            Template template = configuration.getTemplate(templateName);
            File docFile = new File(outPutPath + "//" + htmlPageName);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // 输出文件
            template.process(dataMap, out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
